;; EX-46.scm - Default instrument from SC3 (JMcC)

;; This is a translation of the default instrument at the
;; SuperCollider language (for event streams).  The player is an
;; example of high density, sample accurate lisp level scheduling (you
;; may need to set the server-latency value to not get 'late' messages
;; from the synthesis engine).

(synthdef-store
 (synthdef
  "default"
  ((out 0) (freq 440) (amp 0.1) (pan 0) (gate 1))
  (let ((z (Mul (LPF.ar (Mix (VarSaw.ar (list freq
					      (Add freq (Rand.ir -0.4 0.0))
					      (Add freq (Rand.ir 0.0 0.4))) 
					0 0.3))
			(XLine.kr (Rand.ir 4000 5000) (Rand.ir 2500 3200) 1))
		(Linen.kr gate 0.01 (Mul amp 0.7) 0.3 2))))
    (Out.ar out (Pan2.ar z pan)))))

(define (p time freq amp pan dur)
  (let ((i (nID)))
    (=> time (list "/s_new" "default" i 0 1 
		   "freq" freq 
		   "amp" amp 
		   "pan" pan))
    (=> (+ time dur) (list "/n_set" i "gate" 0))))

(schedule-start)

(let ((i 1/20))
  (@ (lambda (pulse time)
       (p time 
	  (midi-note->frequency (random '(65 67 69 70 72 74 76 77)))
	  (random 0.10 0.15) 
	  (random -0.1 0.1)
	  i)
       i)))

(let ((i 0.0625))
  (@r 
   (yield)
   (let loop ((time (utc)))
     (p time 
	(midi-note->frequency (random '(65 67 69 70 72 74 76 77)))
	(random 0.10 0.15) 
	(random -0.1 0.1)
	i)
     (loop (yield i)))))
